#!/usr/bin/groovy
//------------------------------------------------------------------------------
//  This Source Code Form is subject to the terms of the Mozilla Public
//  License, v. 2.0. If a copy of the MPL was not distributed with this
//  file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
@Library('test-shared-library') _

import ai.h2o.ci.Utils
def utilsLib = new Utils()

pipeline {
    agent none

    // Setup job options
    options {
        ansiColor('xterm')
        timestamps()
        timeout(time: 120, unit: 'MINUTES')
        buildDiscarder(logRotator(daysToKeepStr: '30'))
    }

    stages {
        stage('Build') {
            parallel {
                stage('Build on x86_64-centos7-cuda8.0') {
                    agent {
                        label "linux && docker && !micro"
                    }
                    steps {
                        dumpInfo 'x86_64-centos7-cuda8 Build Info'
                        script {
                            sh """
                                make mrproper_in_docker
                                make BRANCH_NAME=${env.BRANCH_NAME} BUILD_NUM=${env.BUILD_ID} centos7_cuda8_in_docker
                            """
                        }
                        stash includes: 'dist/**/*', name: 'x86_64-centos7-cuda8.0'
                    }
                }
                stage('Build on x86_64-centos7-cuda9.0') {
                    agent {
                        label "linux && docker && !micro"
                    }
                    steps {
                        dumpInfo 'x86_64-centos7-cuda9 Build Info'
                        script {
                            sh """
                                make mrproper_in_docker
                                make BRANCH_NAME=${env.BRANCH_NAME} BUILD_NUM=${env.BUILD_ID} centos7_cuda9_in_docker
                            """
                        }
                        stash includes: 'dist/**/*', name: 'x86_64-centos7-cuda9.0'
                    }
                }
                stage('Build on ppc64le-centos7-cuda8.0') {
                    agent {
                        label "ibm-power"
                    }
                    steps {
                        dumpInfo 'ppc64le-centos7-cuda8 Build Info'
                        script {
                            sh """
                                make mrproper_in_docker
                                make BRANCH_NAME=${env.BRANCH_NAME} BUILD_NUM=${env.BUILD_ID} centos7_cuda8_in_docker
                            """
                        }
                        stash includes: 'dist/**/*', name: 'ppc64le-centos7-cuda8.0'
                    }
                }
                stage('Build on ppc64le-centos7-cuda9.0') {
                    agent {
                        label "ibm-power"
                    }
                    steps {
                        dumpInfo 'ppc64le-centos7-cuda9 Build Info'
                        script {
                            sh """
                                make mrproper_in_docker
                                make BRANCH_NAME=${env.BRANCH_NAME} BUILD_NUM=${env.BUILD_ID} centos7_cuda9_in_docker
                            """
                        }
                        stash includes: 'dist/**/*', name: 'ppc64le-centos7-cuda9.0'
                    }
                }
            }
        }

        stage('Publish centos7 snapshot to S3') {
            when {
                branch 'master'
            }
            agent {
                label "linux && docker && !micro"
            }
            steps {
                sh "rm -rf dist"
                unstash 'x86_64-centos7-cuda8.0'
                unstash 'x86_64-centos7-cuda9.0'
                unstash 'ppc64le-centos7-cuda8.0'
                unstash 'ppc64le-centos7-cuda9.0'
                sh 'echo "Stashed files:" && find dist'
                script {
                    docker.withRegistry("https://docker.h2o.ai", "docker.h2o.ai") {
                        docker.image('s3cmd').inside {
                            def versionText = utilsLib.getCommandOutput("cat dist/x86_64-centos7-cuda8.0/VERSION.txt")
                            s3up {
                                localArtifact = 'dist/*'
                                artifactId = "h2o4gpu"
                                version = versionText
                                keepPrivate = false
                            }
                        }
                    }
                }
            }
        }
    }
}

